//
//  SavedDocument.m
//  Alfresco
//
//  Created by Michael Muller on 10/7/09.
//  Copyright 2010 Zia Consulting.  Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
//

#import "SavedDocument.h"


@implementation SavedDocument

+ (BOOL) isSaved: (NSString *) filename {
	return [[NSFileManager defaultManager] fileExistsAtPath:[SavedDocument pathToSavedFile:filename]];
}

+ (void) save: (NSString *) filename {

	// the source is in the temp dir
	NSString *source = [SavedDocument pathToTempFile:filename];
	
	// the destination is in the documents dir
	NSString *destination = [SavedDocument pathToSavedFile:filename];
	
	NSError *error = nil;
	
	[[NSFileManager defaultManager] copyItemAtPath:source toPath:destination error:&error];
}

// aka "delete" :)
+ (void) unsave: (NSString *) filename {
	
	NSError *error = nil;
	
	[[NSFileManager defaultManager] removeItemAtPath:[SavedDocument pathToSavedFile:filename] error:&error];
}

+ (NSArray *) list {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *docDir = [paths objectAtIndex:0];
	
	NSError *error = nil;
	
	NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docDir error:&error];
	[files retain];
	return files;
}

+ (NSString *) mimeTypeForFilename: (NSString *) filename {
	NSString *mimeType = @"application/octet-stream";
	NSDictionary *mapping = [NSDictionary dictionaryWithObjectsAndKeys:
							 @"image/png", @".png", 
							 @"image/jpg", @".jpeg", 
							 @"image/jpg", @".jpg", 
							 @"image/gif", @".gif", 
							 @"application/msword", @".doc", 
							 @"application/msword", @".docx", 
							 @"application/msexcel", @".xls", 
							 @"application/msexcel", @".xlsx", 
							 @"application/mspowerpoint", @".ppt", 
							 @"application/mspowerpoint", @".pptx", 
							 @"text/xml", @".xml", 
							 @"text/plain", @".txt", 
							 @"application/pdf", @".pdf", 
							 @"application/zip", @".zip", 
							 @"audio/x-mpeg", @".mp3", 
							 @"audio/x-wav", @".wav", 
							 @"text/html", @".html", 
							 @"text/html", @".htm", 
							 @"image/tiff", @".tif", 
							 @"image/tiff", @".tiff", 
							 nil];
	NSUInteger location = [filename rangeOfString:@"." options: NSBackwardsSearch].location;
	if (location != NSNotFound) {
		NSString *ext = [[filename substringFromIndex:location] lowercaseString];
		if ([mapping objectForKey:ext]) {
			mimeType = [mapping objectForKey:ext];
		}
	}
	return mimeType;
}

+ (NSString *) pathToSavedFile: (NSString *) filename {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *docDir = [paths objectAtIndex:0];
	NSString *path = [docDir stringByAppendingPathComponent:filename];
	NSLog(@"path: %@", path);
	// [paths release];
	return path;
}

+ (NSString *) pathToTempFile: (NSString *) filename {
	return [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
}

+ (NSString *) sizeOfSavedFile: (NSString *) filename {
	NSError *error = nil;

	NSString *path = [SavedDocument pathToSavedFile:filename];
	NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];

	NSArray *keys = [attrs allKeys];
	for (NSString *key in keys)
		NSLog(@"  %@ = %@", key, [attrs objectForKey:key]);
	
	NSNumber *size = [attrs objectForKey:NSFileSize];
	return [SavedDocument stringFromFileSize: [size intValue]];
}

+ (NSString *)stringFromFileSize:(int)theSize
{
	float floatSize = theSize;
	if (theSize<1023)
		return([NSString stringWithFormat:@"%i bytes",theSize]);
	floatSize = floatSize / 1024;
	if (floatSize<1023)
		return([NSString stringWithFormat:@"%1.1fk",floatSize]);
	floatSize = floatSize / 1024;
	if (floatSize<1023)
		return([NSString stringWithFormat:@"%1.1fM",floatSize]);
	floatSize = floatSize / 1024;
	
	// Add as many as you like
	
	return([NSString stringWithFormat:@"%1.1fG",floatSize]);
}

@end
